package com.zzw.springcloud.controller;

import com.zzw.springcloud.bean.Member;
import com.zzw.springcloud.service.MemberService;
import com.zzw.springcloud.utils.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

//http://localhost:9090/member/provider/selectById?id=2
@Slf4j
@Controller
@RequestMapping("/member/provider")
public class MemberProviderController {
    @Resource
    private MemberService memberService;

    @GetMapping("/selectById/{id}")
    @ResponseBody
    public Result selectById(@PathVariable("id") Integer id,
                             @RequestParam(value = "name", required = false) String name,
                             @RequestHeader(value = "token", required = false) String token) {
        log.info("id={}, name={}, token={}", id, name, token);

        Member member = memberService.selectById(id);
        //测试OpenFeign调用超时
        //try {
        //    Thread.sleep(5000);
        //} catch (InterruptedException e) {
        //    throw new RuntimeException(e);
        //}
        if (member != null) {
            return Result.success("会员查询成功 member-service-provider 10002 =》" + member);
        } else {
            return Result.error("402", "会员ID=" + id + "不存在");
        }
    }

    @PostMapping("/insert")
    @ResponseBody
    public Result insert(@RequestBody Member member) {
        int insert = memberService.insert(member);
        return Result.success(insert);
    }
}
